#!/usr/bin/env python
# encoding: utf-8
__author__ = '马志'
from xml.dom.minidom import Document

"""

by: 马志 at: 2016/9/13 15:31
@contact: hautmz@163.com
@file: generate_dbxml.py
"""


def create_db_config(db_type, db_name, db_host, db_port, db_username, db_psd):
    """

    by: 马志 at: ${DATE} ${TIME}
    """
    doc = Document()  # 创建DOM文档对象
    sqlMapConfig = doc.createElement('sqlMapConfig')
    sqlMapConfig.setAttribute('xmlns', "http://ibatis.apache.org/dataMapper")
    sqlMapConfig.setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance")
    sqlMapConfig.setAttribute('xsi:schemaLocation', "http://ibatis.apache.org/dataMapper")
    doc.appendChild(sqlMapConfig)
    # 创建settings节点
    settings = doc.createElement('settings')
    sqlMapConfig.appendChild(settings)
    # 创建useStatementNamespaces节点
    setting1 = doc.createElement('setting')
    setting1.setAttribute("useStatementNamespaces", "false")
    settings.appendChild(setting1)
    # 创建useStatementNamespaces节点
    setting2 = doc.createElement('setting')
    setting2.setAttribute("cacheModelsEnabled", "false")
    settings.appendChild(setting2)
    # 创建useStatementNamespaces节点
    setting3 = doc.createElement('setting')
    setting3.setAttribute("validateSqlMap", "false")
    settings.appendChild(setting3)
    # providers
    sqlMapConfig.appendChild(create_providers(doc, db_type))
    sqlMapConfig.appendChild(create_database_xml(doc, db_type, db_name, db_host, db_port, db_username, db_psd))
    sqlMapConfig.appendChild(create_map(doc, db_type))

    return doc


def create_database_xml(doc, db_type, db_name, db_host, db_port, db_username, db_psd):
    """
    
    by: 马志 at: ${DATE} ${TIME}
    """
    database = doc.createElement("database")
    provider = doc.createElement("provider")
    if int(db_type) == 0:
        provider.setAttribute("name", "SQLite3")
    if int(db_type) == 1:
        provider.setAttribute("name", "MySql")
    if int(db_type) == 2:
        provider.setAttribute("name", "sqlServer")
    database.appendChild(provider)
    dataSource = doc.createElement("dataSource")
    if int(db_type) == 0:
        dataSource.setAttribute("name", "FaceDeteiveDB")
        connectionStringValue = "Data Source=%s;Version=3;Password=%s;" % (db_name, db_psd)
        dataSource.setAttribute("connectionString", connectionStringValue)
    if int(db_type) == 1:
        dataSource.setAttribute("name", "IBatisNet")
        connectionStringValue = "Host=%s;UserName=%s;Password=%s;Database=%s;Port=%s;CharSet=utf8;Allow Zero Datetime=true" % (
            db_host, db_username, db_psd, db_name, db_port)
        dataSource.setAttribute("connectionString", connectionStringValue)
    if int(db_type) == 2:
        dataSource.setAttribute("name", "IBatisNet")
        connectionStringValue = "Server=%s,%s; User ID=%s;Password=%s;Database=%s;Persist Security Info=True" % (
            db_host, db_port, db_username, db_psd, db_name)
        dataSource.setAttribute("connectionString", connectionStringValue)
    database.appendChild(dataSource)
    return database


def create_map(doc, db_type):
    sqlMaps = doc.createElement("sqlMaps")
    sqlMap1 = doc.createElement("sqlMap")
    sqlMap2 = doc.createElement("sqlMap")
    if int(db_type) == 0:
        sqlMap1.setAttribute("resource", "Sqlite1/BlankFeatureDao.xml")
        sqlMap2.setAttribute("resource", "Sqlite1/LogDao.xml")
    if int(db_type) == 1:
        sqlMap1.setAttribute("resource", "MySql/BlankFeatureDao.xml")
        sqlMap2.setAttribute("resource", "MySql/LogDao.xml")
    if int(db_type) == 2:
        sqlMap1.setAttribute("resource", "SqlServer/BlankFeatureDao.xml")
        sqlMap2.setAttribute("resource", "SqlServer/LogDao.xml")
    sqlMaps.appendChild(sqlMap1)
    sqlMaps.appendChild(sqlMap2)
    return sqlMaps


def create_providers(doc, db_type):
    """
    
    by: 马志 at: ${DATE} ${TIME}
    """
    providers = doc.createElement("providers")
    if int(db_type) == 0:
        providers.setAttribute("resource", "sqlite_providers.config")
    if int(db_type) == 1:
        providers.setAttribute("resource", "mysql_providers.config")
    if int(db_type) == 2:
        providers.setAttribute("resource", "sqlserver_providers.config")
    return providers
